package com.h3bpm.kumhosunny.crm.controller.budget;

import OThinker.H3.Controller.ControllerBase;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import com.h3bpm.kumhosunny.crm.controller.enums.DateEnum;
import data.DataRow;
import data.DataTable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @authormi
 * @date2019/11/8
 */

public class BudgetSumSoloController extends ControllerBase {


    private static final int THREE =3 ;
    private static final int TWO =2 ;
    private static final int FOUR =4 ;
    private static   String  bmonth ="bmonth" ;
    public static  int getSeasonNumber(String  month){

        if(DateEnum.JANUARY.getText().equals(month) || DateEnum.FEBRUARY.getText().equals(month) ||  DateEnum.MARCH.getText().equals(month)){
            return 1;
        } else  if(DateEnum.APRIL.getText().equals(month) || DateEnum.MAY.getText().equals(month) ||  DateEnum.JUNE.getText().equals(month)){
            return 2;
        } else if(DateEnum.JULY.getText().equals(month) || DateEnum.AUGUST.getText().equals(month) ||  DateEnum.SEPTEMBER.getText().equals(month)){
            return 3;
        } else if(DateEnum.OCTOBER.getText().equals(month) || DateEnum.NOVEMBER.getText().equals(month) ||  DateEnum.DECEMBER.getText().equals(month)){
            return 4;
        }
        return 0;
    }

    public static int getJdNumber(String month ){

        if(month.equals(DateEnum.JANUARY.getText())  ||  month.equals(DateEnum.APRIL.getText())  ||  month.equals(DateEnum.JULY.getText()) || month.equals(DateEnum.OCTOBER.getText())){
            return 1;
        }
        else if(month.equals(DateEnum.FEBRUARY.getText())  ||  month.equals(DateEnum.MAY.getText())  ||  month.equals(DateEnum.AUGUST.getText()) || month.equals(DateEnum.NOVEMBER.getText())){

            return 2;
        }
        else  if(month.equals(DateEnum.MARCH.getText())  ||  month.equals(DateEnum.JUNE.getText())  ||  month.equals(DateEnum.SEPTEMBER.getText()) || month.equals(DateEnum.DECEMBER.getText())){
            return 3;
        }

        return 0;
    }


    public  static void updateDyBudget(DataTable dataTable, DataRow dataRow,String thisMonth,String userId,  String year, String oneMonth , String towMonth, String threeMonth) throws Exception {


        BigDecimal availableBudget1 =null;
        BigDecimal availableBudget2 =null;
        BigDecimal availableBudget3 =null;
        for(DataRow dataRowChild : dataTable.getEntityRows()) {
            if(dataRowChild.getString(bmonth).equals(oneMonth)  &&  dataRowChild.getString("userId").equals(userId)) {
                availableBudget1 = dataRowChild.getBigDecimal("availableBudget");
            }
            if(dataRowChild.getString(bmonth).equals(towMonth) &&  dataRowChild.getString("userId").equals(userId)) {
                availableBudget2 = dataRowChild.getBigDecimal("availableBudget");
            }
            if(dataRowChild.getString(bmonth).equals(threeMonth) &&  dataRowChild.getString("userId").equals(userId)) {
                availableBudget3 = dataRowChild.getBigDecimal("availableBudget");
            }
        }
        if(dataRow.getString(bmonth).equals(thisMonth)) {

            SimpleDateFormat simpleDateFormatUpdate=new SimpleDateFormat("yyyy-MM-dd");
            Date dateTime = new Date();
            String date = simpleDateFormatUpdate.format(dateTime);
            String[] dateArray =  date.split("-");
            String month = dateArray[1];
            int m = getJdNumber(month);
            if(m==1){
                String updateDubgetSql = "update k_dubgetexpand set lastMonthMoney = '"+availableBudget1.toString()+"',dyMonthBudget = '"+availableBudget1+"' where  userId='"+userId+"' and  byear='"+year+"' and  bmonth = '"+oneMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql);
            }
            if(m==TWO){
                String updateDubgetSql = "update k_dubgetexpand set lastMonthMoney = '"+availableBudget1.toString()+"',dyMonthBudget = '"+availableBudget1+"' where  userId='"+userId+"' and  byear='"+year+"' and  bmonth = '"+oneMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql);

                BigDecimal bigAvailableBudget =  availableBudget2.add(availableBudget1);
                String updateDubgetSql2 = "update k_dubgetexpand set lastMonthMoney = '"+availableBudget1.toString()+"',dyMonthBudget = '"+bigAvailableBudget+"' where  userId='"+userId+"' and byear='"+year+"' and  bmonth = '"+towMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql2);
            }
            if(m==THREE){
                String updateDubgetSql = "update k_dubgetexpand set lastMonthMoney = '"+availableBudget1.toString()+"',dyMonthBudget = '"+availableBudget1+"' where  userId='"+userId+"' and  byear='"+year+"' and  bmonth = '"+oneMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql);

                BigDecimal bigAvailableBudget =  availableBudget2.add(availableBudget1);
                String updateDubgetSql2 = "update k_dubgetexpand set lastMonthMoney = '"+availableBudget1.toString()+"',dyMonthBudget = '"+bigAvailableBudget+"' where  userId='"+userId+"' and byear='"+year+"' and  bmonth = '"+towMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql2);


                BigDecimal lastAvailableBudget =  availableBudget2.add(availableBudget1);
                BigDecimal bigAvailableBudget2 =  availableBudget3.add(availableBudget2).add(availableBudget1);
                String updateDubgetSql3 = "update k_dubgetexpand set lastMonthMoney = '"+lastAvailableBudget.toString()+"',dyMonthBudget = '"+bigAvailableBudget2+"' where   userId='"+userId+"' and  byear='"+year+"' and  bmonth = '"+threeMonth+"'";
                SqlUtil.doNoQuery(updateDubgetSql3);
            }


        }




    }

    public static  void updatePracticalAvailableBudget(String date ,String userId) throws Exception {

        String[] dateArray =  date.split("-");
        String month = dateArray[1];
        int  quarter =  getSeasonNumber(month);
        if(quarter==1){
            String getBudgetsql ="Select * from k_dubgetexpand  where userId='"+userId+"' and  byear='"+dateArray[0]+"' and  (bmonth = '01' or bmonth = '02' or bmonth = '03')";
            DataTable dataTable =  SqlUtil.doQuery(getBudgetsql);
            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0) {
                for (DataRow dataRow : dataTable.getEntityRows()) {
                    updateDyBudget(dataTable, dataRow, dateArray[1], userId, dateArray[0], "01", "02", "03");
                }
            }
        }
        if(quarter==TWO){
            String getBudgetsql ="Select * from k_dubgetexpand  where userId='"+userId+"' and  byear='"+dateArray[0]+"' and  (bmonth = '04' or bmonth = '05' or bmonth = '06')";
            DataTable dataTable =  SqlUtil.doQuery(getBudgetsql);
            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0) {
                for (DataRow dataRow : dataTable.getEntityRows()) {
                    updateDyBudget(dataTable, dataRow, dateArray[1], userId, dateArray[0], "04", "05", "06");
                }
            }
        }
        if(quarter==THREE){
            String getBudgetsql ="Select * from k_dubgetexpand  where  userId='"+userId+"' and byear='"+dateArray[0]+"' and  (bmonth = '07' or bmonth = '08' or bmonth = '09')";
            DataTable dataTable =  SqlUtil.doQuery(getBudgetsql);
            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0) {
                for (DataRow dataRow : dataTable.getEntityRows()) {
                    updateDyBudget(dataTable, dataRow, dateArray[1], userId, dateArray[0], "07", "08", "09");
                }
            }
        }
        if(quarter==FOUR){
            String getBudgetsql ="Select * from k_dubgetexpand  where userId='"+userId+"' and  byear='"+dateArray[0]+"' and  (bmonth = '10' or bmonth = '11' or bmonth = '12')";
            DataTable dataTable =  SqlUtil.doQuery(getBudgetsql);
            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0) {
                for (DataRow dataRow : dataTable.getEntityRows()) {
                    updateDyBudget(dataTable, dataRow, dateArray[1], userId, dateArray[0], "10", "11", "12");

                }
            }
        }


    }


    @Override
    public String getFunctionCode() {
        return null;
    }
}
